const express = require("express");
const path = require('path');
const app = express();
//引入错误中间件
const error = require("./error.js")
// open自动打开网页的第三方包
const open = require("open")
const artTemplate = require('art-template')
const expressTemplate = require('express-art-template')

app.set('views',__dirname+'/view/')         
app.engine('html',expressTemplate);
app.set('view engine','html')

//托管静态资源(把图片,文件的路径改为绝对路径)
app.use(express.static("view"));

 
app.get('/',(req,res)=>{
    let swiper = [
        "/css/img/lbt1.jpg",
        "/css/img/lbt2.jpg",
        "/css/img/lbt3.jpg",
        "/css/img/lbt4.png",
        "/css/img/lbt5.png",
        "/css/img/lbt6.png",
        "/css/img/lbt7.jpg",
        "/css/img/lbt8.png",
    ]
    let center = [
        {img:"/css/img/centerBottom1.png",text:"苏宁秒杀"},
        {img:"/css/img/centerBottom2.png",text:"苏宁超市"},
        {img:"/css/img/centerBottom3.png",text:"苏宁拼购"},
        {img:"/css/img/centerBottom4.png",text:"手机数码"},
        {img:"/css/img/centerBottom5.png",text:"苏宁家电"},
        {img:"/css/img/centerBottom6.png",text:"免费水果"},
        {img:"/css/img/centerBottom7.png",text:"赚钱消消乐"},
        {img:"/css/img/centerBottom8.png",text:"签到有礼"},
        {img:"/css/img/centerBottom9.png",text:"领劵中心"},
        {img:"/css/img/centerBottom10.png",text:"更多频道"},
    ]
    let recommend = [
        {img1:"/css/img/recommendTop1.png",img2:"/css/img/recommendTop11.png",text:"冰洗钜惠",activity:"抢1000元劵"},
        {img1:"/css/img/recommendTop2.png",img2:"/css/img/recommendTop22.png",text:"电视家影",activity:"预定抢半价"},
        {img1:"/css/img/recommendTop3.png",img2:"/css/img/recommendTop33.png",text:"手机通讯",activity:"至高24期免息"},
        {img1:"/css/img/recommendTop4.png",img2:"/css/img/recommendTop44.png",text:"个户美颜",activity:"买赠好礼"},
    ]
    let recommendBottom = [
        {text1:"天天低价",text2:"人气好货限时抢",img1:"/css/img/recommendMiddle1.jpg",img2:"/css/img/recommendMiddle2.jpg",price1:"￥1.1",price2:"￥1.49"},
        {text1:"苏宁秒杀",text2:"品质好货天天秒",img1:"/css/img/recommendMiddle3.jpg",img2:"/css/img/recommendMiddle4.jpg",price1:"汇尔康(HR)",price2:"飞科(FLYCO)"},
        {text1:"百亿补贴",text2:"大牌补贴天天有",img1:"/css/img/recommendMiddle5.jpg",img2:"/css/img/recommendMiddle6.jpg",price1:"补贴价1579",price2:"补贴价629"},
        {text1:"苏宁拼购",text2:"2人拼更实惠 全场包邮",img1:"/css/img/recommendMiddle7.jpg",img2:"/css/img/recommendMiddle8.jpg",price1:"2人拼54",price2:"2人拼799"},
    ]
    let selected = [
        {img:"/css/img/recommendMiddle9.jpg",text:"苏宁国际"},
        {img:"/css/img/recommendMiddle10.jpg",text:"苏宁Outl..."},
        {img:"/css/img/recommendMiddle11.jpg",text:"新品首发"},
        {img:"/css/img/recommendMiddle12.jpg",text:"0元试用"},
    ]
    let bottom = [

        {text:"逆龄活肤精华版",list:"人气榜",salesVolume:"卖爆8085件",img1:"/css/img/recommendBottom1.jpg",img2:"/css/img/recommendBottom2.jpg"},
        {text:"高弹耐穿袜子版",list:"人气榜",salesVolume:"卖爆1.6万件",img1:"/css/img/recommendBottom3.jpg",img2:"/css/img/recommendBottom4.jpg"},
        {text:"多功能养生壶版",list:"趋势榜",salesVolume:"本周趋势上升66%",img1:"/css/img/recommendBottom5.jpg",img2:"/css/img/recommendBottom6.jpg"},
        {text:"人气热销手机榜",list:"人气榜",salesVolume:"卖爆9.4万件",img1:"/css/img/recommendBottom7.jpg",img2:"/css/img/recommendBottom8.jpg"},
    ]
    let bottomFixed = [
        {img:"/css/img/bottomfixed1.png",text:"猜你喜欢"},
        {img:"/css/img/bottomfixed2.png",text:"分类"},
        {img:"/css/img/bottomfixed3.png",text:"排行榜"},
        {img:"/css/img/bottomfixed4.png",text:"购物车"},
        {img:"/css/img/bottomfixed5.png",text:"我的易购"}
    ]
    // 读取文件内容并响应给浏览器
    // res.sendFile(path.join(__dirname,'view/shunin.html')) 
    res.render('shunin.html',{swiper,center,recommend,recommendBottom,selected,bottom,bottomFixed})
})


// ** 如果上面的路由都没有匹配到就执行这个中间件(因为匹配到了路由就不会再往下执行get,post请求和中间件)
app.use((req,res,next)=>{
    res.status(404).sendFile( path.join(__dirname,'view/404.html') )
    let err = new Error("错了")
    next(err)
})

app.use(error)


    // app.use((err,req,res,next)=>{
    //     console.log("错了");
    //     next()交给下一个继续执行
    //     })


app.listen(5000,()=> console.log('server is running at port 5000') )

// 执行到这里时打开这个网址,要写到app.listen的回调函数里
// open('http://127.0.0.1:4000');
